package com.uxwine.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class CycleButton extends ImageView
{
	public CycleButton(Context context)
	{
		super(context);
		initView();
	}
	
	public CycleButton(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView();
	}	

	@Override
	public boolean dispatchTouchEvent(MotionEvent event)
	{
		final int action = event.getAction();
		switch(action)
		{
		case MotionEvent.ACTION_DOWN:
			if(!isInCycle(event.getX(), event.getY()))
				return false;
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			break;
		default:
			break;
		}
		return super.dispatchTouchEvent(event);
	}
	
	protected boolean isInCycle(final float x, final float y)
	{
		final float w = getMeasuredWidth();
		final float h = getMeasuredHeight();
		
		final float ox = w / 2;
		final float oy = h / 2;
		final float r = Math.min(ox, oy);
		
		return (x - ox) * (x - ox) + (y - oy) * (y - oy) <= r * r;
	}

	protected void initView()
	{
		setClickable(true);
	}

}
